<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      v-if="isMain"
    >
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="唯一纳税识别号" prop="companyTin">
        <el-input
          v-model="queryParams.companyTin"
          placeholder="请输入唯一纳税识别号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="行政区划" prop="regionTownship">
        <!-- <el-input
          v-model="queryParams.regionTownship"
          placeholder="请选择乡镇"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="queryParams.regionTownship"
          :options="addressOption"
          @change="addressHandleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="人员类型" prop="certificationUserType">
        <el-select
          v-model="queryParams.certificationUserType"
          placeholder="请选择认证人员类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.certification_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="approved">
        <!-- <el-input
          v-model="queryParams.approved"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.approved"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.certification_approve_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="审核时间" prop="approvedTime">
        <el-date-picker clearable
          v-model="queryParams.approvedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="isMain">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enterprise:certification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['enterprise:certification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['enterprise:certification:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['enterprise:certification:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="certificationList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column
        label="唯一纳税识别号"
        align="center"
        prop="companyTin"
        v-if="isMain"
      />
      <el-table-column
        label="行政区划"
        align="center"
        prop="address"
        v-if="isMain"
      >
        <template slot-scope="scope">
          {{ scope.row.regionProvinceStr }}{{ scope.row.regionCityStr
          }}{{ scope.row.regionDistrictStr }}{{ scope.row.regionTownshipStr }}
        </template>
      </el-table-column>
      <el-table-column
        label="认证人员类型"
        align="center"
        prop="certificationUserType"
        v-if="isMain"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.certification_user_type"
            :value="scope.row.certificationUserType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="认证人员"
        align="center"
        prop="certificationUserId"
        v-if="isMain"
      />
      <el-table-column
        label="认证人员姓名"
        align="center"
        prop="certificationUserName"
      />
      <el-table-column label="审核状态" align="center" prop="approved">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.certification_approve_status"
            :value="scope.row.approved"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="审核时间"
        align="center"
        prop="approvedTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvedTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['enterprise:certification:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['enterprise:certification:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleCheck(scope.row, 'view')"
            v-hasPermi="['enterprise:certification:remove']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.approved === '0'"
            @click="handleCheck(scope.row, 'check')"
            v-hasPermi="['enterprise:certification:remove']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.approved === '1'"
            @click="handleCheck(scope.row, 'change')"
            v-hasPermi="['enterprise:certification:changeUser']"
            >人员变更</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="isMain"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="唯一纳税识别号" prop="companyTin">
          <el-input
            v-model="form.companyTin"
            placeholder="请输入唯一纳税识别号"
          />
        </el-form-item>
        <el-form-item label="行政区划-省" prop="regionProvince">
          <el-input
            v-model="form.regionProvince"
            placeholder="请输入行政区划-省"
          />
        </el-form-item>
        <el-form-item label="行政区划-市" prop="regionCity">
          <el-input v-model="form.regionCity" placeholder="请输入行政区划-市" />
        </el-form-item>
        <el-form-item label="行政区划-区\县" prop="regionDistrict">
          <el-input
            v-model="form.regionDistrict"
            placeholder="请输入行政区划-区\县"
          />
        </el-form-item>
        <el-form-item label="行政区划-乡镇\街道" prop="regionTownship">
          <el-input
            v-model="form.regionTownship"
            placeholder="请输入行政区划-乡镇\街道"
          />
        </el-form-item>
        <el-form-item
          label="认证人员类型(字典certification_user_type)"
          prop="certificationUserType"
        >
          <el-select
            v-model="form.certificationUserType"
            placeholder="请选择认证人员类型(字典certification_user_type)"
          >
            <el-option
              v-for="dict in dict.type.certification_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="认证人员ID" prop="certificationUserId">
          <el-input
            v-model="form.certificationUserId"
            placeholder="请输入认证人员ID"
          />
        </el-form-item>
        <el-form-item label="企业营业执照附件地址" prop="companyBlUrl">
          <el-input
            v-model="form.companyBlUrl"
            placeholder="请输入企业营业执照附件地址"
          />
        </el-form-item>
        <el-form-item label="企业授权书附件地址" prop="companyLoaUrl">
          <el-input
            v-model="form.companyLoaUrl"
            placeholder="请输入企业授权书附件地址"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="approved">
          <el-input v-model="form.approved" placeholder="请输入审核状态" />
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectedReason">
          <el-input
            v-model="form.rejectedReason"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 弹窗  显示/审核/变更  企业信息  -->
    <el-dialog
      :title="
        handleType === 'view'
          ? '查看'
          : handleType === 'check'
          ? '审核'
          : '人员变更'
      "
      :visible.sync="dialogVisible"
      width="980px"
      class="dialog"
      append-to-body
    >
      <div>
        <el-divider content-position="left">企业基本信息</el-divider>
        <el-form
          ref="form"
          :model="companyForm"
          label-width="180px"
          :inline="true"
          size="medium"
        >
          <el-form-item label="企业名称">
            <el-input
              v-model="companyForm.companyName"
              disabled
              style="width: 250px"
            ></el-input>
          </el-form-item>
          <el-form-item label="唯一纳税识别号">
            <el-input
              v-model="companyForm.companyTin"
              disabled
              style="width: 250px"
            ></el-input>
          </el-form-item>
          <el-form-item label="企业行政区域">
            <el-input
              v-model="companyForm.address"
              disabled
              style="width: 250px"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="认证人员类别" prop="certificationUserType">
            <el-select
              v-model="companyForm.certificationUserType"
              disabled
              style="width: 250px"
            >
              <el-option
                v-for="dict in dict.type.certification_user_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="企业法人/代理人名称">
            <el-input
              v-model="companyForm.certificationUserName"
              disabled
              style="width: 250px"
            >
              <el-button
                v-show="handleType === 'change'"
                :slot="handleType === 'change' ? 'append' : ''"
                @click="changeUser"
                >变更</el-button
              >
            </el-input>
          </el-form-item>

          <el-divider content-position="left">企业资质文件</el-divider>

          <el-form-item label="企业营业执照">
            <ImagePreview
              :width="250"
              :src="companyForm.companyBlUrl"
            ></ImagePreview>
          </el-form-item>
          <el-form-item label="企业授权书">
            <ImagePreview
              :width="250"
              :src="companyForm.companyLoaUrl"
            ></ImagePreview>
          </el-form-item>
        </el-form>
        <!-- <div class="company">
          <div class="content">
            <div>企业营业执照</div>
            <ImagePreview :src="companyForm.companyBlUrl"></ImagePreview>
          </div>
          <div class="content">
            <div>企业授权书</div>
            <ImagePreview :src="companyForm.companyLoaUrl"></ImagePreview>
          </div>
        </div> -->
      </div>
      <el-divider content-position="left" v-if="handleType === 'check'"
        >驳回原因</el-divider
      >
      <div class="reject" v-if="handleType === 'check'">
        <el-input
          v-model="rejectForm.remark"
          type="textarea"
          style="width: 100%"
          :rows="4"
          placeholder="请输入驳回原因"
        ></el-input>
      </div>
      <el-divider></el-divider>
      <span v-if="handleType === 'check'" slot="footer" class="dialog-footer">
        <el-button type="error" @click="handleReject">审核驳回</el-button>
        <el-button type="primary" @click="handlePass">审核通过</el-button>
      </span>
      <span v-if="handleType === 'view'" slot="footer" class="dialog-footer">
        <el-button type="error" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 人员弹窗 -->
    <el-dialog :visible.sync="userVisible" width="80%" append-to-body>
      <UserInfo :change="false" @rowRadioHandle="rowRadioHandle" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="userCancel">取消</el-button>
        <el-button type="primary" @click="userConfirm">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCertification,
  approved,
  approveRejected,
  changeUser,
  delCertification,
  findNxLevel4Tree,
  getCertification,
  listCertification,
  updateCertification,
} from "@/api/enterprise/certification";
import UserInfo from "../../user/info/index.vue";
export default {
  name: "Certification",
  dicts: [
    "certification_user_type",
    "certification_approve_status",
    "certification_approve_status",
  ],
  components: {
    UserInfo,
  },
  props: {
    isMain: {
      type: Boolean,
      default: true, // 默认为true  展示  为false时 为首页的组件
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业管理表格数据
      certificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 操作弹窗
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approved: null,
        companyName: null,
        companyTin: null,
        regionTownship: null,
        certificationUserType: null,
        approved: null,
        approvedTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
      },
      addressOption: [],
      // 操作弹窗 控制显示的值
      handleType: "",
      // 显示表单
      companyForm: {},
      // 驳回表单
      rejectForm: {},
      // 人员
      userVisible: false,
      // 点击获取的人员信息
      userInfo: {},
    };
  },
  created() {
    this.queryParams.approved = this.isMain ? null : 0;
    this.getList();
    this.getAddressData();
  },
  methods: {
    addressHandleChange(value) {
      const tips = value[value.length - 1];
      console.log("value", tips);
      this.queryParams.regionTownship = tips;
      // this.getList();
    },

    //
    getAddressData() {
      findNxLevel4Tree().then((res) => {
        const addressOption = this.transformAddressData(res.data);
        this.addressOption = addressOption;
      });
    },
    transformAddressData(data) {
      return data.map((item) => {
        return {
          value: item.id,
          label: item.name,
          children: item.hasChild
            ? this.transformAddressData(item.children)
            : undefined,
        };
      });
    },
    //
    changeUser() {
      this.userVisible = true;
    },

    rowRadioHandle(Row) {
      this.userInfo = Row;
    },
    // 变更人员
    userConfirm() {
      const data = {
        id: this.companyForm.id,
        userId: this.userInfo.id,
      };
      changeUser(data).then(() => {
        this.getList();
        this.userVisible = false;
        this.dialogVisible = false;
        this.handleType = "";
      });
    },
    // 取消
    userCancel() {
      console.log("取消");
      this.userVisible = false;
    },
    // 审核通过
    handlePass() {
      const id = this.companyForm.id;
      const data = {
        ids: [id],
      };
      approved(data).then(() => {
        this.getList();
        this.dialogVisible = false;
        this.handleType = "";
      });
    },
    // 审核驳回
    handleReject() {
      const remark = this.rejectForm.remark;
      if (!remark) {
        this.$message("驳回原因为必填");
        return;
      }
      const id = this.companyForm.id;
      const data = {
        ids: [id],
        reason: this.rejectForm.remark,
      };
      approveRejected(data).then(() => {
        this.getList();
        this.dialogVisible = false;
        this.handleType = "";
      });
    },

    /** 查询企业管理列表 */
    getList() {
      this.loading = true;
      listCertification(this.queryParams).then((response) => {
        // console.log('response',response);
        // return;
        // const data = {
        //   ...response.rows,
        //   address: response.rows.regionProvinceStr + response.rows.regionCityStr + response.rows.regionDistrictStr + response.rows.regionTownshipStr
        // }
        this.certificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        companyTin: null,
        regionProvince: null,
        regionCity: null,
        regionDistrict: null,
        regionTownship: null,
        certificationUserType: null,
        certificationUserId: null,
        certificationUserName: null,
        certificationUserIdCard: null,
        certificationUserMobile: null,
        companyBlUrl: null,
        companyLoaUrl: null,
        approved: null,
        approvedTime: null,
        approvedBy: null,
        unApprovedTime: null,
        unApprovedBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        mapLongitude: null,
        mapLatitude: null,
        rejectedReason: null,
        regionTownship: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCertification(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCertification(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertification(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除企业管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCertification(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /**  审核/查看/变更  操作按钮   */
    handleCheck(row, type) {
      this.rejectForm = {};
      this.companyForm = {};
      const id = row.id || this.ids;
      getCertification(id).then((response) => {
        this.handleType = type;
        const data = {
          ...response.data,
          address:
            response.data.regionProvinceStr +
            response.data.regionCityStr +
            response.data.regionDistrictStr +
            response.data.regionTownshipStr,
        };
        this.companyForm = data;
        console.log("response.data", response.data);
        this.dialogVisible = true;
        // this.title = "修改企业管理";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "enterprise/certification/export",
        {
          ...this.queryParams,
        },
        `certification_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="less" scoped>
.company {
  display: flex;
  justify-content: space-between;
  // border: 1px solid red;
  padding: 20px 80px;
  .content {
    width: 48%;
    // display: flex;
  }
}

.reject {
  margin: 10px 80px;
  // border: 1px solid red;
}
.dialog {
  ::v-deep .el-dialog__body {
    padding: 0 20px;
  }
}
.el-divider--horizontal {
  margin: 12px 0;
}
</style>
